package com.jinwei.qunguanjia.web.group.job;
import com.jinwei.qunguanjia.user.dto.UserDTO;
import com.jinwei.qunguanjia.user.service.UserService;
import com.jinwei.qunguanjia.web.tencent.push.PushController;
import com.jinwei.qunguanjia.web.user.controller.UserController;
import org.apache.http.util.TextUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component("GroupRecommendJob")
public class GroupRecommendJob {

    @Resource
    private UserController userController;

    @Resource
    private UserService userService;

    // 每天早上9点执行任务
    @Scheduled(cron = "0 34 16 * * ?")
    public void sendDailyPushNotifications() {
        PushController pushController = new PushController();
        // 获取所有用户的token列表
        ArrayList<String> tokenList = getAllUserTokens();

        // 调用推送方法
        pushController.pushTokenIos(tokenList);
//        pushController.pushTokenAndroid(tokenList);
    }

    private ArrayList<String> getAllUserTokens() {
        // 调用UserService的方法来获取用户列表
        List<UserDTO> userList = userService.listAll();
        ArrayList users = new ArrayList<String>();
        // 打印用户列表（仅作为示例）
        for (UserDTO user : userList) {
            if (!TextUtils.isEmpty(user.getDeviceToken())) {
                users.add(user.getDeviceToken());
            }
        }
        return users;
    }
}
